var pageNum = 1;
var order = 0;
var descNo = BIG_NO;
var ascNo = undefined;
$(function () {
    $(".select-group").click(function () {
        $(".up-down-icon").toggleClass("active");
        $(".group-list-mask").toggleClass("show");
    });
    $(".order-wrapper").click(function () {
        $(".order-icon-wrapper").toggleClass("active");
        $(".chapter-list-wrapper ul li").remove();
        $(".load-wrapper").hide();
        $(".no-more").hide();
        pageNum = 1;
        if(order == 0){
            order = 1;
            console.log(descNo);
            getChapList(pageNum,order,descNo);
        }else{
            order = 0;
            getChapList(pageNum,order,ascNo);
        }

    });
    $(".back-wrapper").click(function () {
        location.href = "./detail.html?id="+getQueryString("id")+"&channel="+getQueryString("channel");
    });
    getChapterInfo();
    getChapList(pageNum,0);
    scrollEvent();
});
function getChapterInfo() {
    $.ajax({
        type:'get',
        url:BASE_URl + "chap/info?id=" + getQueryString("id"),
        success:function (data) {
            if(data.success){
                DrawChapGroup(data.data.total);
            }
        }
    });
}

function DrawChapGroup(total) {
    if(total <= 100){
        var text = "1-" + total + "章";
        $(".group-text").html(text);
    }else{
        $(".group-text").html("1-100章");
        var row = Math.ceil(total/100);
        var html = "";
        for(var i = 0;i < row;i ++){
            if(i == (row - 1)){
                html += "<li onclick='selectGroup(" + (i * 100 + 1) + "," + total +")'>" + (i * 100 + 1) + "-" + total + "章" + "</li>";
            }else{
                html += "<li onclick='selectGroup(" + (i * 100 + 1) + "," + (i * 100 + 100) +")'>" + (i * 100 + 1) + "-" + (i * 100 + 100) + "章" + "</li>";
            }
        }
        $(".group-list-wrapper ul").html(html);
    }
}

function selectGroup(min,max) {
    $(".group-text").html(event.target.innerHTML);
    pageNum = 1;
    $(".load-wrapper").hide();
    $(".no-more").hide();
    if(order == 0){
        $(".chapter-list-wrapper ul li").remove();
        ascNo = min - 1;
        descNo = max + 1;
        getChapList(pageNum,order,min-1);
    }else{
        $(".chapter-list-wrapper ul li").remove();
        descNo = max + 1;
        ascNo = min - 1;
        getChapList(pageNum,order,descNo);
    }
    $(".group-list-mask").toggleClass("show");
}

function getChapList(page,sort,chapterNum) {
    var url = "";
    if(chapterNum == undefined){
        url = "chap/list?id=" + getQueryString("id") + "&page=" + page + "&limit=20&sort=" + sort
    }else{
        url = "chap/list?id=" + getQueryString("id") + "&page=" + page + "&limit=20&sort=" + sort + "&no=" + chapterNum
    }
    $.ajax({
        type:'get',
        url:BASE_URl + url,
        success:function (data) {
            $(".load-wrapper").hide();
            if(data.success){
                pageNum ++ ;
                DrawChapList(data.data);
            }else{
                $(".no-more").show();
            }
        }
    });
}

function DrawChapList(data) {
    var html = "";
    var current = getQueryString("current");
    for (var i = 0;i < data.length;i ++){
        if(data[i].id == current){
            html += `<li onclick="goRead('${data[i].id}','${data[i].logo}')">
                        <p class="chapter-name inline active">${data[i].name}</p>
                        <i class="type-icon inline ${data[i].logo}"></i>
                     </li>`;
        }else{
            html += `<li onclick="goRead('${data[i].id}','${data[i].logo}')">
                        <p class="chapter-name inline">${data[i].name}</p>
                        <i class="type-icon inline ${data[i].logo}"></i>
                     </li>`;
        }
    }
    $(".chapter-list-wrapper ul").append(html);
}

function scrollEvent() {
    window.onscroll = function(){
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;  // 获取滚动的距离
        //var total = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight); // 文档总高度
        var total = $(".chapter-list-wrapper").height();
        var clientHeight = document.body.clientHeight && document.documentElement.clientHeight; // 工作区高度
        if(scrollTop == 0){
            return;
        }
        if(clientHeight + scrollTop == total){
            $(".no-more").hide();
            $(".load-wrapper").show();
            if(order == 1){
                getChapList(pageNum,order,descNo);
            }else{
                getChapList(pageNum,order,ascNo);
            }
        }
    };
}

function goRead(chapterId,type) {
    location.href = "./read.html?id="+getQueryString("id")+"&cid="+chapterId+"&channel="+getQueryString("channel");
}